﻿using BySoft.Server.Common;
using BySoft.Server.Common.Entity;
using BySoft.Server.DataAccess;
using BySoft.Server.DataAccess.UserCode;
using OneSoft.DataAccess;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace BySoft.Server.Business.UserCode
{
    public partial class BsPurchaseBusiness
    {
        public static DataTable SelectBody(string strCode, DataAccessBroker broker)
        {
            return BsPurchaseDataAccess.SelectBody(strCode, broker);
        }

        public static decimal SelectResNum(string strRquisitionDetailID, string strCode)
        {
            return BsPurchaseDataAccess.SelectResNum(strRquisitionDetailID, strCode);
        }
        public static void SaveOrUpdate(BsPurchaseEntity Entity, DataTable DtPurchaseData, string Type)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                broker.BeginTransaction();
                try
                {
                    if (Type == "Save")
                    {
                        DataAccessCommon.Insert(Entity, broker);
                    }
                    else
                    {
                        DataAccessCommon.UpdateOnly(Entity, broker);
                    }
                    if (DtPurchaseData != null && DtPurchaseData.Rows.Count > 0)
                    {
                        foreach (DataRow drItem in DtPurchaseData.Rows)
                        {
                            BsPurchasedetailEntity en = new BsPurchasedetailEntity();
                            en.BillID = Entity.BillID;
                            en.MaterialID = CommonMethod.GetString(drItem["MaterialID"]);
                            en.OrderNum = CommonMethod.ConvertToDecimal(drItem["OrderNum"]);
                            if (!string.IsNullOrEmpty(CommonMethod.GetString(drItem["RquisitionID"])))
                            {
                                en.RquisitionID = CommonMethod.GetString(drItem["RquisitionID"]);
                            }
                            if (!string.IsNullOrEmpty(CommonMethod.GetString(drItem["RquisitionDetailID"])))
                            {
                                en.RquisitionDetailID = CommonMethod.ConvertToDecimal(drItem["RquisitionDetailID"]);
                            }
                            en.WorkOrderNo = CommonMethod.GetString(drItem["WorkOrderNo"]);
                            en.MaterialID = CommonMethod.GetString(drItem["MaterialID"]);
                            en.Nums = CommonMethod.ConvertToDecimal(drItem["Nums"]);
                            en.MMNumber = CommonMethod.ConvertToDecimal(drItem["MMNumber"]);
                            en.Price = CommonMethod.ConvertToDecimal(drItem["Price"]);
                            en.Amount = CommonMethod.ConvertToDecimal(drItem["Amount"]);
                            en.ExpectDate = Convert.ToDateTime(drItem["ExpectDate"]);

                            string strIsNew = CommonMethod.GetString(drItem["IsNew"]);
                            string strIsDel = CommonMethod.GetString(drItem["IsDel"]);
                            if (strIsNew == "1" && strIsDel != "1")
                            {
                                DataAccessCommon.Insert(en, true, broker);
                            }
                            if (strIsNew != "1" && strIsDel != "1")
                            {
                                en.BillDetailID = CommonMethod.ConvertToInt(drItem["BillDetailID"]);
                                DataAccessCommon.UpdateOnly(en, broker);
                            }
                            if (strIsNew != "1" && strIsDel == "1")
                            {
                                //删除明细
                                ClsCommDA.Del("BS_PurchaseDetail", "BillDetailID", CommonMethod.GetString(drItem["BillDetailID"]), broker);
                            }
                        }
                    }
                    broker.Commit();
                }
                catch
                {
                    broker.Rollback();
                    throw;
                }
            }
        }

        public static DataTable SelectMaterial(string MaterialID, string MaterialName, string IsRquisition, string RquisitionID, int pageIndex, int PageSize, out int m_count)
        {
            return BsPurchaseDataAccess.SelectMaterial(MaterialID, MaterialName, IsRquisition, RquisitionID, pageIndex, PageSize, out m_count);
        }



        public static void Del(string strCode)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    ClsCommDA.Del("BS_PurchaseDetail", "BillID", strCode, broker);
                    ClsCommDA.Del("BS_Purchase", "BillID", strCode, broker);
                    broker.Commit();
                }
                catch
                {
                    broker.Rollback();
                    throw;
                }
            }
        }

        public static DataTable SelectList(string BillID, string strFrom, string strTo, string WorkOrderNo, string MaterialName,
            int IPurchaseCount, out int icurPageIndex, int pageIndex, int pageSize, out int m_count)
        {
            return BsPurchaseDataAccess.SelectList(BillID, strFrom, strTo, WorkOrderNo, MaterialName, 
                IPurchaseCount, out icurPageIndex, pageIndex, pageSize, out m_count);
        }

        public static DataTable GetDataByBillID(string id)
        {
            return BsPurchaseDataAccess.GetDataByBillID(id);
        }
    }
}
